package com.anxin.act.utils;

import com.anxin.act.process.service.ActProcessService;
import com.anxin.act.task.vo.TaskVo;
import com.anxin.common.utils.StringUtils;
import com.anxin.common.utils.spring.SpringUtils;
import com.anxin.sys.record.entity.SysUpdateRecord;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.runtime.ProcessInstance;

import java.util.HashMap;
import java.util.Map;

public class ActProcessUtils {
    private static RuntimeService runtimeService = SpringUtils.getBean(RuntimeService.class);
    private static HistoryService historyService = SpringUtils.getBean(HistoryService.class);
    private static ActProcessService actProcessService = SpringUtils.getBean(ActProcessService.class);

    /**
     * 获取流程实例对象
     *
     * @param actId 流程实例ID
     * @return 流程实例对象
     */
    public static ProcessInstance getProcessInstance(String actId) {
        return runtimeService.createProcessInstanceQuery().processInstanceId(actId).includeProcessVariables().singleResult();
    }

    /**
     * 获取流程标题
     *
     * @param actId 流程实例ID
     * @return 待办标题
     */
    public static String getProcessTitle(String actId) {
        ProcessInstance processInstance = ActProcessUtils.getProcessInstance(actId);
        if (processInstance != null) {
            return processInstance.getName();
        }
        return ActProcessUtils.getHiProcessInstance(actId).getName();
    }

    /**
     * 获取流程申请人
     *
     * @param actId 流程实例ID
     * @return 流程申请人登陆名
     */
    public static String getApplicant(String actId) {
        return ActProcessUtils.getHiProcessInstance(actId).getStartUserId();
    }

    /**
     * 获取历史流程实例对象
     *
     * @param actId 流程实例ID
     * @return
     */
    public static HistoricProcessInstance getHiProcessInstance(String actId) {
        HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery().includeProcessVariables().processInstanceId(actId).singleResult();
        return historicProcessInstance;
    }

    /**
     * 根据流程实例ID 获取流程定义ID
     *
     * @param actId 流程实例ID
     * @return
     */
    public static String getProcessDefinitionId(String actId) {
        ProcessInstance processInstance = ActProcessUtils.getProcessInstance(actId);
        if (processInstance != null) {
            return processInstance.getProcessDefinitionId();
        }
        return ActProcessUtils.getHiProcessInstance(actId).getProcessDefinitionId();
    }

    /**
     * 根据流程实例ID 获取流程定义Key
     *
     * @param actId 流程实例Key
     * @return
     */
    public static String getProcessDefinitionKey(String actId) {
        ProcessInstance processInstance = ActProcessUtils.getProcessInstance(actId);
        if (processInstance != null) {
            return processInstance.getProcessDefinitionKey();
        }
        return ActProcessUtils.getHiProcessInstance(actId).getProcessDefinitionKey();
    }

    /**
     * 获取流程申请人
     *
     * @return
     */
    public static String getProcessApplicant(String actId) {
        Object applyUserId = ActUtils.getProcessVar(actId).get("applyUserId");
        return applyUserId != null ? applyUserId.toString() : null;
    }

    /**
     * 获取流程变量
     *
     * @param key
     * @return
     */
    public static String getProcessVar(String actId, String key) {
        Object value = ActUtils.getProcessVar(actId).get(key);
        return value != null ? value.toString() : null;
    }


    /**
     * 流程启动时设置流程中需要传递的参数
     * @param processInstanceId
     * @return
     */
    public static Map<String,String> setParameters(String processInstanceId) {
        Map<String, String> map = new HashMap<>();
        //获取流程变量
        Map<String, Object> processVar = ActUtils.getProcessVar(processInstanceId);
        if(processVar!=null && processVar.get("actDataTable")!=null){
            switch (processVar.get("actDataTable").toString()){
                case "meetings_manage":
                    //通用办公-会议申请 设置参数
                    Map<String, Object> stringObjectMap = actProcessService.selectData(processVar.get("actDataTable").toString(), processVar.get("actDataId").toString());
                    if (stringObjectMap.get("meeting_type") != null) {
                        map.put("meetingType", stringObjectMap.get("meeting_type").toString());
                    }
                    break;
                default:
            }
        }
        return map;
    }

    /**
     * 提交时 设置流程中需要传递的参数
     * @param taskVo
     * @return
     */
    public static Map<String,String> completeSetParameters(TaskVo taskVo) {
        Map<String, String> map  = ActProcessUtils.setParameters(taskVo.getActId());
        map.put("bh","");
        //判断是否是驳回
        if(taskVo!=null && taskVo.getOpinions()!=null
                &&  taskVo.getOpinions().get("approved")!=null && "不同意".equals(taskVo.getOpinions().get("approved"))){

            //如果是不同意，则查询节点ID 是否存在 bh-节点 格式 进行设置驳回参数
            if(StringUtils.isNotBlank(taskVo.getTaskKey()) && taskVo.getTaskKey().contains("-bh-")){
                String taskKey = taskVo.getTaskKey();
                String taskId = taskKey.split("-")[taskKey.split("-").length - 1];
                if(StringUtils.isNotBlank(taskId)){
                    map.put("bh",taskId);
                }
            }
        }

        return map;
    }

}
